<?php
namespace App\Repos;

use App\Models\Area;
use Redis;

class AreaRepo 
{
    /**
     * 省份信息
     */
    public static function getProvince()
    {
    	if(Redis::exists("fruit:provinces")){
    		$list = json_decode(Redis::get("fruit:provinces"));
    		return $list;
    	}  
    	$list = Area::select('num','name')
    				->where(['parent_num'=>0])
    				->get()
    				->toArray();
    	Redis::set('fruit:provinces',json_encode($list));
    	return $list;			
    }

    /**
     * 下级区域
     */
    public static function subArea($parent_num)
    {
    	if(Redis::exists("fruit:subarea_".$parent_num)){
    		$list = json_decode(Redis::get("fruit:subarea_".$parent_num));
    		return $list;
    	}

    	$list = Area::select('num','name')
    			->where(['parent_num'=>$parent_num])
    			->get()
    			->toArray();
    	Redis::set("fruit:subarea_".$parent_num,json_encode($list));
    	return $list;		
    }
}
